<template>
  <div class="policy">
    <el-dialog
      :title="title"
      :visible="dialogPolicy"
      width="40%"
      @close="closeFn('addPolicy')"
      @open="openFn"
    >
      <el-form
        ref="addPolicy"
        label-width="120px"
        :model="addPolicy"
        :rules="addPolicyRules"
      >
        <el-form-item label="策略名称" prop="policyName">
          <el-input
            v-model="addPolicy.policyName"
            type="text"
            placeholder="请输入内容"
            maxlength="15"
            show-word-limit
            style="width: 80%"
          />
        </el-form-item>
        <el-form-item label="策略方案" prop="discount">
          <el-input-number
            v-model="addPolicy.discount"
            style="width: 80%"
            controls-position="right"
            :min="0"
            :max="100"
            placeholder="请输入"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancel" @click="closeFn('addPolicy')">取 消</el-button>
        <el-button
          type="primary"
          class="sure"
          @click="PolicyFn"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { postPolicy, putPolicy } from '@/api/policy'
export default {
  props: {
    dialogPolicy: {
      type: Boolean,
      default: () => {}
    },
    item: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      addPolicy: {
        policyName: '',
        discount: ''
      },
      addPolicyRules: {
        policyName: [{ required: true, message: '请输入', trigger: 'blur' }],
        discount: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.item.policyId ? '修改策略' : '新增策略'
    }

  },
  methods: {
    // 1.策略弹出层取消
    closeFn(addPolicy) {
      this.addPolicy = {
        policyName: '',
        discount: ''
      }
      this.$refs[addPolicy].resetFields()
      this.$emit('closeFn')
    },
    // 1.2.策略管理弹出层确定提交数据&&重新刷新表格数据
    async PolicyFn() {
      this.$refs.addPolicy.validate(async(isOk) => {
        if (isOk) {
          if (this.item.policyId) {
            console.log('我是修改')
            this.addPolicy.policyId = this.item.policyId
            await putPolicy(this.addPolicy)
          } else {
            console.log('我是新增')
            await postPolicy(this.addPolicy)
          }
          this.$refs.addPolicy.resetFields()
          this.$emit('PolicyFn')
        }
      })
    },
    // 1.3.点击查看详情拿到每一行数据做一个数据回写然后再修改
    openFn() {
      if (this.item.policyId) {
        this.addPolicy.policyName = this.item.policyName
        this.addPolicy.discount = this.item.discount
      }
    }
  }
}
</script>

<style lang="scss">
.policy {
  .el-dialog {
    border-radius: 10px;
    .el-dialog__header .el-dialog__title {
      color: #333333;
      font-size: 16px;
      font-weight: bold;
    }
    .el-dialog__header {
      border-radius: 10px;
      background-color: #fff;
    }
    .el-icon-close:before {
      color: #909399;
    }
    .el-dialog__body {
      padding: 15px 20px;
    }
    .dialog-footer {
      padding: 0 20px 10px 20px;
      .cancel {
        background-color: #fbf4f0 !important;
        border: unset;
        padding-top: 10px;
        height: 36px;
        width: 80px !important;
      }
      .sure {
        background: linear-gradient(135deg, #ff9743, #ff5e20);
        border: unset;
        color: #fff;
        height: 36px;
        width: 80px !important;
      }
    }
    .el-input {
      .el-input__inner {
        height: 36px !important;
        &::placeholder {
          text-align: left;
        }
      }
    }
    .el-link.el-link--default {
      color: #5f84ff;
    }
  }
}
</style>
